package com.quasar.hpatient.bean.home_temperature;

import android.text.TextUtils;

import com.quasar.hpatient.R;

import lib.quasar.context.BaseConstant;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.chart.entry.Entry;

/**
 * description: 日常记录, 体温
 * created by kalu on 2018/4/24 17:00
 */
public class TemperatureItemBean implements Entry {

    private String record_datetime = "";
    private String record_value = "";
    private String record_dose = "";
    private String record_type = "0";

    public String getRecord_datetime() {
        return record_datetime;
    }

    public void setRecord_datetime(String record_datetime) {
        this.record_datetime = record_datetime;
    }

    public String getRecord_value() {
        if (!TextUtils.isEmpty(record_value) && record_value.length() >= 2 && record_value.endsWith("."))
            return record_value.substring(0, record_value.length() - 1);
        else if (".".equals(record_value)) {
            return "";
        } else {
            return record_value;
        }
    }

    public void setRecord_value(String record_value) {
        this.record_value = record_value;
    }

    public String getRecord_dose() {
        return record_dose;
    }

    public void setRecord_dose(String record_dose) {
        this.record_dose = record_dose;
    }

    public String getRecord_type() {
        return record_type;
    }

    public void setRecord_type(String record_type) {
        this.record_type = record_type;
    }

    @Override
    public String getAxisX() {
        return getLogtime(true);
    }

    @Override
    public float getAxisYMax() {
        return 0;
    }

    @Override
    public float getAxisYMin() {
        return 0;
    }

    @Override
    public String getUnit() {
        return BaseConstant.NULL_STR;
    }

    public String getLogtime(boolean onlyHour) {

        if (!TextUtils.isEmpty(record_datetime) && record_datetime.length() != 18) {
            return record_datetime.substring(onlyHour ? 11 : 0, 16);
        } else {
            return "时间错误";
        }
    }

    @Override
    public float getValue() {

        try {
            return Float.parseFloat(record_value);
        } catch (Exception e) {

            e.printStackTrace();
            return 0f;
        }
    }

    @Override
    public float[] getValues() {
        return null;
    }

    public int getColor() {

        try {
            float temp = Float.parseFloat(record_value);
            return (temp >= 35.7f && temp <= 37.3f) ? R.color.color_bg_theme : R.color.color_bg_peach;
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
            return R.color.color_bg_peach;
        }
    }

    public int getTemperatureColor() {

        try {
            float temp = Float.parseFloat(record_value);
            return (temp >= 35.7f && temp <= 37.3f) ? R.color.color_black : R.color.color_bg_peach;
        } catch (Exception e) {

            LogUtil.e("", e.getMessage(), e);
            return R.color.color_bg_peach;
        }
    }

    public boolean isWarning() {
        return !getState().startsWith("正常");
    }

    public boolean isRight() {
        try {
            float temp = Float.parseFloat(record_value);
            return (temp >= 35f && temp <= 43f);
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
            return false;
        }
    }

    public String getState() {
        try {
            float temp = Float.parseFloat(record_value);

            // ≤35.6为体温过低
            if (temp <= 35.6f) {
                return "体温过低";
            }
            // 35.7——37.3为正常
            else if (temp >= 35.7f && temp <= 37.3f) {
                return "正常";
            }
            // 37.4——38.1为低热
            else if (temp >= 37.4f && temp <= 38.1f) {
                return "低热";
            }
            // ≥38.2为高热
            else {
                return "高热";
            }

        } catch (Exception e) {

            LogUtil.e("", e.getMessage(), e);
            return "异常信息";
        }
    }

    @Override
    public String getDate() {
        try {
            final int length = record_datetime.length();
            return record_datetime.substring(length - 8, length - 3);
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
            return "未知时间";
        }
    }

    @Override
    public String getName() {
        return null;
    }

    public String getKey() {
        if (!TextUtils.isEmpty(record_datetime) && record_datetime.length() != 18) {
            return record_datetime.substring(0, 11) + "00:00:00";
        } else {
            return "0000-00-00 00:00:00";
        }
    }

    public boolean isLessTime(String datetime) {

        /**
         * 如果参数字符串等于此字符串，则返回值 0；
         * 如果此字符串小于字符串参数，则返回一个小于 0 的值；
         * 如果此字符串大于字符串参数，则返回一个大于 0 的值。
         */
        return record_datetime.compareToIgnoreCase(datetime) < 0;
    }
}
